<?php


namespace app\api\controller\v21;


use app\api\controller\v13\Auth2;
use app\common\cachemodel\ZlcrAstr;
use app\common\cachemodel\ZlcrCustomReportGoods;
use app\common\cachemodel\ZlcrCustomReportQuestion;
use app\common\cachemodel\ZlcrTag;
use app\common\lib\Util;
use Exception;
use think\Env;
use think\response\Json;

/**
 * @version 2.5.0
 * 定制报告相关
 * Class CustomReport
 * @package app\api\controller\v21
 */
class CustomReport extends Auth2
{

    protected $completeAnswerTime;//接单后回答完成时长

    protected function _initialize()
    {
        parent::_initialize();
        $this->completeAnswerTime = Env::get('queue.custom_report_answer6_delay');
    }

    /**
     * 定制报告首页
     * @return Json
     * @throws Exception
     */
    public function index()
    {
        $ossHost = Env::get('host.photo');
        $ordersModel = new \app\common\cachemodel\Orders();
        $questionModel = new ZlcrCustomReportQuestion();
        $questionGoodsModel = new ZlcrCustomReportGoods();
        $astrModel = new ZlcrAstr();
        //获取报告解锁数量
        $unlockNum = $ordersModel->getTotalPrepaidOrder(15);
        $report_status_list = [];
        if ($this->user)
        {
            //获取当前用户已支付报告进行的状态
            $questionReportInfo = $questionModel->getUserQuestionReportFromCache($this->user['id'], 1, [2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15, 16], [0]);
            if ($questionReportInfo['data']['data'])
            {
                foreach ($questionReportInfo['ids'] as $item)
                {
                    //获取报告状态文案和跳转地址
                    $statusLink = $questionModel->getReportStateDes($questionReportInfo['data']['data'][$item]);
                    unset($statusLink['order_des']);
                    $report_status_list[] = $statusLink;
                }
            }
        }
        //获取热门问题
        $questionGoodsInfo = $questionGoodsModel->getQuestionGoodsFromCache(1);
        //获取五条最近已完成回复的占星师资料，根据 last_reply_time
        $astrInfo = $astrModel->getAstrInfoFromCache(5, ['nickname', 'avatar', 'rating', 'extend', 'astr_type']);
        $star_state = [];
        foreach ($astrInfo as $key => $astr)
        {
            //获取用户对占星师的评价
            if ($astr['astr_type'] == 1)
            {
                $tag = $astrModel->getAstrTagInfo($key);
            } else
            {
                $tag = $astr['extend'];
            }

            $tag = floor($astr['rating']) . '%好评 ｜ ' . $tag;

            $star_state[] = [
                'title' => $astr['nickname'] . '完成了定制报告',
                'avatar' => $astr['avatar'],
                'tips' => $tag,
                'rating' => (int)($astr['rating']) . '%好评'
            ];
        }
        $fixed_question_list = [];//固定问题
        $custom_question_list = [];//自定义问题
        if ($questionGoodsInfo['data'])
        {
            foreach ($questionGoodsInfo['ids'] as $value)
            {
                if (!isset($questionGoodsInfo['data'][$value]['id']))
                {
                    continue;
                }
                $report_lst = [
                    'gid' => (int)$questionGoodsInfo['data'][$value]['id'],
                    'question_name' => $questionGoodsInfo['data'][$value]['question_name'],
                    'is_merge' => (int)$questionGoodsInfo['data'][$value]['is_merge'],
                    'price' => (int)$questionGoodsInfo['data'][$value]['price'],
                    'question_type' => (int)$questionGoodsInfo['data'][$value]['qtype'],
                ];
                //固定问题
                if ($questionGoodsInfo['data'][$value]['qtype'] == 1)
                {
                    $fixed_question_list[] = $report_lst;
                } //自定义问题固定最后一位
                else
                {
                    $custom_question_list[] = $report_lst;
                }
            }
        }
        $indexNum = $questionGoodsModel->getGoodsIndexNum();
        if ($indexNum['switch'])
        {
            $hot_question_num_str = '今日限量剩余：' . $indexNum['num'];
        }
        $data = [
            'title' => '定制报告',
            'title_img' => $ossHost . 'custom_reports/img/custom_report_index.png',
            'unlock_num' => $unlockNum + $indexNum['base_unlock_num'],
            'report_status_list' => $report_status_list,
            'hot_question' => [
                'title' => '热门问题',
                'question_list' => array_merge($fixed_question_list, $custom_question_list)
            ],
//            'star_state' => [
//                'title' => '占星师动态',
//                'state_list' => $star_state,
//            ],//先隐藏
            'star_state' => [
                'title' => '',
                'state_list' => [],
            ],//先隐藏
            'img_tag' => $ossHost . 'custom_reports/img/astr_tag_list.png?v=1',
            'hot_question_num_str' => $hot_question_num_str ?? '',
            'is_show_icon' => $indexNum['is_show_icon'] ?? 0
        ];
        return show(200, '请求成功', $data);
    }

    /**
     * 获取定制报告所有问题
     * @throws Exception
     */
    public function getQuestion()
    {
        $reportGoodsModel = new ZlcrCustomReportGoods();
        $apiHost = Env::get('host.domain');
        $data = $reportGoodsModel->getQuestionGoodsFromCache(2);
        $data = $reportGoodsModel->getQuestionAllsResrt($data['ids'], $data['data']);
        return show(config('code.success'), '请求成功', [
            'example_data' => [
                'name' => '报告示例',
                'target' => 'link',
                'link' => $apiHost . '/web/app_demo/custom_report?need_auth=0',
                'status' => 1,
            ],
            'tab_list' => $data
        ]);
    }

    /**
     * 获取定制报告下单示例数据
     * @return Json
     * @throws Exception
     */
    public function getCustomReportPayOrderDemo()
    {
        $gid = input('gid/d', 0);
        if (!$gid)
        {
            return show(0, '请求参数异常');
        }
        $questionGoods = (new ZlcrCustomReportGoods())->getInfoById($gid);
        if (!$questionGoods)
        {
            return show(config('code.error'), '数据不存在');
        }
        $tag = (new ZlcrTag())->getInfoById($questionGoods['tid'], ['tag_name']);
        $astrInfo = (new ZlcrAstr())->getAstrInfoFromCache(10, ['avatar','extend']);
        $data = [
            'question_name' => $questionGoods['question_name'],
            'question_desc' => $questionGoods['description'],
            // 'tips' => '注：8:00-16:00的提问，将在接单后6小时内得到回复，16:00-次日8:00的提问，将在次日8:00开始计时，6小时内得到回复',
            'tips' => '',
            'astr_avatar' => $astrInfo ? array_column($astrInfo, 'avatar') : [],//todo 已完成报告的占星师头像
            'astr_desc' => '您的问题将由准了至臻占星团队产出专业定制报告',
            'is_merge' => $questionGoods['is_merge'],
            'question_type' => $questionGoods['qtype'],
            'example_url' => $questionGoods['example_url'],
            'title' => $tag['tag_name'] . '问题'
        ];
        return show(200, '请求成功', $data);
    }

    /**
     * 获取定制报告标签
     * @return Json
     * @throws Exception
     */
    public function getCustomReportTag()
    {
        $tagModel = new ZlcrTag();
        $tag = $tagModel->getLevelTag(1, 0);
        $tag = $tagModel->getQuestionAllsResrt($tag);
        return show(200, '请求成功', $tag);
    }
}
